import { Component, OnInit, Host, ViewChild } from '@angular/core';
import { AccountsService } from '../accounts.service';
import { AppComponent } from '../../../../app.component';
import { ActivatedRoute } from '@angular/router';
import { AccountModel } from '../models/accounts.model';
import { TableOptions } from '../../../../shared/components/table/models/tableOptions.model';
import { PaginationPositionDisplayEnum, TableComponent } from '../../../../shared/components/table/table.component';
import { ColumnOptions, ColumnType } from '../../../../shared/components/table/models/columnOptions.model';
import { Page } from '../../../../shared/components/table/models/pagination.model';
import { TableButton, ButtonType } from '../../../../shared/components/table/models/tableButton.model';
import swal, { SweetAlertOptions } from 'sweetalert2';
import { ToasterService } from 'angular2-toaster';
import { StatusEnum } from '../../models/user.model';

@Component({
    templateUrl: './account.component.html',
    providers: [AccountsService]
})
export class AccountComponent implements OnInit {
    private options: TableOptions;
    public selectedUsers: string[] = [];
    public pageSize = 5;
    public account: AccountModel;
    public count: number;
    public users: any[];
    public statuses: typeof StatusEnum = StatusEnum;
    public selectedStatus: StatusEnum = StatusEnum.Active;
    public paginationPosition = PaginationPositionDisplayEnum.Down;
    public columns: ColumnOptions[] = [
        new ColumnOptions('selected', 'Select', 'col-md-2', false, ColumnType.Buttons),
        new ColumnOptions('name', 'Full Name', 'col-md-4', false, ColumnType.Label),
        new ColumnOptions('email', 'Username', 'col-md-4', false, ColumnType.Label),
        new ColumnOptions('status', 'Status', 'col-md-2', false, ColumnType.Label)
    ];
    @ViewChild('usersTable') usersTable: TableComponent;

    constructor(@Host() private rootComponent: AppComponent,
        private route: ActivatedRoute,
        private accountsService: AccountsService,
        private toasterService: ToasterService) { }

    ngOnInit() {
        this.options = new TableOptions('name', this.pageSize);
        this.route.params.subscribe(param => {
            this.accountsService.getAccount(param['id']).subscribe(data => {
                this.account = data;
                this.rootComponent.finishLoading();
                this.getAccountUsers();
            });
        });
    }

    public getAccountUsers(listOnly: boolean = false) {
        if (listOnly == false) {
            if (this.options.pageOptions.skip > 0) this.usersTable.pageChanged(new Page(0, this.pageSize));
            this.accountsService.getAccountUserCount(this.account.id, this.selectedStatus).subscribe(data => {
                this.count = data;
            });
        }

        this.accountsService.getAccountUserList(this.account.id, this.selectedStatus, this.options.pageOptions.skip, this.options.pageOptions.take).subscribe(data => {
            this.users = data.map(user => {
                return {
                    selected: [new TableButton('selected', { id: user.id, selected: this.selectedUsers.indexOf(user.id) > -1 }, true, ButtonType.CheckBox)],
                    name: user.fullName,
                    email: user.email,
                    status: user.status,
                    state: false
                }
            });
        })
    }

    public tableOptionsChanged(tableOptions: TableOptions) {
        this.options = tableOptions;
        this.getAccountUsers(tableOptions.pageOptions.listOnly);
    }

    public onUserSelectionChanged(event: any) {
        if (event.selected == false)
            this.selectedUsers.push(event.id);
        else
            this.selectedUsers.splice(this.selectedUsers.indexOf(event.id), 1);
    }

    public resetPass() {
        const swalOptions: SweetAlertOptions = {
            buttonsStyling: false,
            customClass: 'modal-content',
            showCancelButton: true,
            title: 'Reset Password',
            text: 'The password will be reset for ' + this.selectedUsers.length + ' user(s). Are you sure that you want to continue?',
            confirmButtonClass: 'btn btn-primary',
            confirmButtonText: 'Reset',
            cancelButtonClass: 'btn btn btn-outline-dark-grey'
        };
        swal(swalOptions).then(result => {
            if (result && result.value)
                this.accountsService.resetPass(this.selectedUsers).subscribe(_ => {
                    this.toasterService.pop('success', 'Success', 'Password(s) reset successfull.');
                });
        });
    }
}